import emitter from '../../../mixins/emitter'

export const propertyChangeHandler = (id, key, value, context) => {
  emitter.methods.dispatch(
    'MainBoard',
    'field-property-changed',
    [id, key, value],
    context
  )
}

export const fieldChangedHandler = (id, value, context) => {
  emitter.methods.dispatch(
    'MainBoard',
    'field-property-changed',
    [id, context.props['shortField'], value],
    context
  )
}

export const getDefaultValueComponent = (type) => {
  let component
  switch (type) {
    case 'textarea':
      component = require('./widgets/textarea').default
      break
    default:
      component = require('./widgets/input').default
      break
  }
  return component
}

// 配置属性都是以 `${namespace}.${field}` 的格式组成
export const parseField = full => {
  const arr = full.split('.')
  const length = arr.length
  if (length > 2) {
    return {
      namespace: arr.slice(0, -1),
      field: arr[length - 1]
    }
  } else if (length === 2) {
    return {
      namespace: arr[0],
      field: arr[1]
    }
  } else {
    return {
      namespace: '',
      field: full
    }
  }
}

export const propsExt = {
  'form.options' (setting) {
    return {
      multiple: setting.options.multiple,
      options: setting.options.options,
      model: setting.options.defaultValue
    }
  },
  'grid.justify' () {
    return {
      options: [
        { label: '左对齐', value: 'start' },
        { label: '右对齐', value: 'end' },
        { label: '居中', value: 'center' },
        { label: '两侧间隔相等', value: 'space-around' },
        { label: '两端对齐', value: 'space-between' },
      ]
    }
  },
  'grid.align' () {
    return {
      options: [
        { label: '顶部对齐', value: 'top' },
        { label: '居中', value: 'middle' },
        { label: '底部对齐', value: 'bottom' },
      ]
    }
  },
  'grid.columns' () {
    return {
      max: 24,
      min: 0
    }
  },
  'form.buttonType' () {
    return {
      options: [
        { label: 'default', value: 'default' },
        { label: 'primary', value: 'primary' },
        { label: 'dashed', value: 'dashed' },
        { label: 'text', value: 'text' },
        { label: 'info', value: 'info' },
        { label: 'success', value: 'success' },
        { label: 'warning', value: 'warning' },
        { label: 'error', value: 'error' },
      ]
    }
  },
  'form.buttonShape' () {
    return {
      options: [
        { label: 'default', value: 'default' },
        { label: 'circle', value: 'circle' },
      ]
    }
  },
  'form.buttonSize' () {
    return {
      options: [
        { label: 'large', value: 'large' },
        { label: 'default', value: 'default' },
        { label: 'small', value: 'small' },
      ]
    }
  }
}
export const FieldQueryHandler = (context) => {
  emitter.methods.dispatch(
    'MainBoard',
    'field-query-handler',
    [context.props['shortField'], '参数'],
    context
  )
}

export const getUiItemExtraForEditable = context => {
  const { editable, selected, index } = context.props
  const extra = {
    'class': {},
    nativeOn: {},
    key: index
  }
  if (editable) {
    extra.class['zlst-ui-item-wrapper'] = true
    extra.nativeOn.click = evt => {
      emitter.methods.dispatch('MainBoard', 'widget-selected', [index], context)
      evt.stopPropagation()
    }
    if (selected) {
      extra.class['is-active'] = true
    }
  }
  return extra
}

export const generateOperateButtons = (h, context) => {
  const commonDataObject = {
    props: { shape: 'circle' }
  }
  const buttons = [
    h('Button', _.merge({}, commonDataObject, {
      props: { icon: 'md-copy', type: 'primary' },
      attrs: { role: 'copy' },
      on: {
        click: evt => {
          emitter.methods.dispatch('MainBoard', 'widget-copy', [context.props.index], context)
          // 禁止冒泡
          evt.stopPropagation()
        }
      }
    })),
    h('Button', _.merge({}, commonDataObject, {
      props: { icon: 'md-trash', type: 'warning' },
      attrs: { role: 'delete' },
      on: {
        click: evt => {
          emitter.methods.dispatch('MainBoard', 'widget-delete', [context.props.index], context)
          // 禁止冒泡
          evt.stopPropagation()
        }
      }
    }))
  ]
  return buttons
}
